<?php


if (!function_exists('arr2str_en')) {
    /**
     *
     * 数组转化为字符串，并且加密，在转化为16进制字符串，填充标志OPENSSL_PKCS1_PADDING
     *
     * @param $arr
     *
     * @return string
     */
    function arr2str_en($arr)
    {
        $res = [];
        foreach ($arr as $field => $item) {
            $res[] = strtoupper($field) . '=' . $item;
        }
        $str = implode('&', $res);
        return bin2hex(openssl_encrypt($str, 'AES-128-ECB', config('services.receive.key'), OPENSSL_PKCS1_PADDING));
    }
}

if (!function_exists('str2arr_de')) {
    /**
     * 16进制字符串转化ASCII码，再解密，并且转化为 数组，填充标志OPENSSL_PKCS1_PADDING
     *
     * @param $str
     *
     * @return array
     */
    function str2arr_de($str)
    {
        $str = openssl_decrypt(hex2bin($str), 'AES-128-ECB', config('services.receive.key'), OPENSSL_PKCS1_PADDING);
        $result = [];
        if ($str) {
            $array = explode('&', $str);
            foreach ($array as $item) {
                [$key, $value] = explode('=', $item);
                $result[strtolower($key)] = $value;
            }
        }
        return $result;
    }
}